﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using MP3Ganizer.Modules.FileView.ViewModels;
using System.Threading;

namespace MP3Ganizer.Modules.FileView.Views
{
  /// <summary>
    /// Interaction logic for FileViewMainV.xaml
  /// </summary>
  public partial class FileViewMainV : UserControl, IFileViewMainV
  {
    #region Fields
    private readonly IUnityContainer _container;
    #endregion

    /// <summary>
    /// Constructeur.
    /// </summary>
    /// <param name="container"></param>
    public FileViewMainV(IUnityContainer container)
    {
      this._container = container;
      InitializeComponent();
    }

    #region IFileViewMainV Members

    /// <summary>
    /// Fonction pour associer un ViewModel à cette vue.
    /// </summary>
    /// <param name="model"></param>
    public void SetModel(MP3Ganizer.Modules.FileView.ViewModels.IFileViewListVM model)
    {
      this.DataContext = model;
    }

    #endregion

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MP3Ganizer.Modules.FileView.ViewModels.IFileViewListVM model = this.DataContext as MP3Ganizer.Modules.FileView.ViewModels.IFileViewListVM;
       
    }

    private void SetArtiste_Click(object sender, RoutedEventArgs e)
    {
        MenuItem artist = sender as MenuItem;
        Dg1.SelectedItems.Cast<IFileViewVM>().ToList<IFileViewVM>().ForEach(x => x.ArtisteAlbum = artist.Header as string);
    }

    private void SetAlbum_Click(object sender, RoutedEventArgs e)
    {
        MenuItem album = sender as MenuItem;
        Dg1.SelectedItems.Cast<IFileViewVM>().ToList<IFileViewVM>().ForEach(x => x.Album = album.Header as string);
    }

    private void Apply_Click(object sender, RoutedEventArgs e)
    {
        Events.User.ShowMediaData data = new Events.User.ShowMediaData("", "", "", "", Common.Enums.DataType.Unknown);
        this._container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Publish(new Events.User.UserActionEventArgs(Common.Enums.UserAction.ShowMedia, data, Apply));
    }

    private void Apply()
    {
        Dg1.SelectedItems.Cast<IFileViewVM>().ToList<IFileViewVM>().ForEach(x => x.ApplyChanges());
    }

    private void Copy_Click(object sender, RoutedEventArgs e)
    {
        Events.User.ShowMediaData data = new Events.User.ShowMediaData("", "", "", "", Common.Enums.DataType.Unknown);
        this._container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Publish(new Events.User.UserActionEventArgs(Common.Enums.UserAction.ShowMedia, data, Copy));
    }
    private void Copy()
    {
        MP3Ganizer.Modules.FileView.ViewModels.IFileViewListVM model = this.DataContext as MP3Ganizer.Modules.FileView.ViewModels.IFileViewListVM;        
        Dg1.SelectedItems.Cast<IFileViewVM>().ToList<IFileViewVM>().ForEach(x =>
                    {
                        x.ApplyChanges();
                        x.CopyToDestination(model);
                    });
    }

    private void Dg1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        IFileViewVM vm = Dg1.SelectedItem as IFileViewVM;
        if (vm != null)
            vm.ShowFile();
    }
  }

  public class IntToColorConverter : IValueConverter
  {
      public object Convert(object value, Type targetType,
              object parameter, System.Globalization.CultureInfo culture)
      {
          if (value == null || !(value is int))
              return null;
          //FileView.ViewModels.FileViewVM vm = (int)value as FileView.ViewModels.FileViewVM;
          //double status = vm.Status;
          double status = (int)value;
          double colorR, colorG, colorB = 0;
          colorR = status < 50 ? 255 : (2 - (status / 50)) * 255;
          colorG = status > 50 ? 255 : (status / 50) * 255;
          colorB = 0;
          SolidColorBrush brush = new SolidColorBrush(Color.FromRgb((byte)colorR, (byte)colorG, (byte)colorB));
          return brush;
      }

      public object ConvertBack(object value, Type targetType,
              object parameter, System.Globalization.CultureInfo culture)
      {
          throw new System.NotImplementedException();
      }
  }
}
